/**
 * Author: w景洋.
 * Time: 2016/4/19 0019
 */
angular.module('service.order', [
        "ngResource",
        "ngStorage",
    ])
    // 订单服务
    .service('OrderService', ['$resource', '$localStorage', 'MallAuthService', function ($resource, $localStorage, MallAuthService) {
        var $storage = $localStorage.$default({
            name: '',
            userId: '',
            wxpay_openid: '',
            order: '',
            orderid: '',
        });
        var userid = MallAuthService.getUserid();
        var order_res = $resource(mallorder + 'orders/:id');
        var order_res1 = $resource(mallorder + 'orderlist/bybuyid/:buyid/page');
        var order_users = $resource(mallorder + 'orders/bybuyid/:id');
        var order_paystatus = $resource(mallorder + 'orderlist/bybuyid/:bybuyid/paystate/:paystate/page');
        var order_status = $resource(mallorder + 'orderlist/bybuyid/:bybuyid/status/:status/page');
        var update = $resource(mallorder + 'orders/:orderid');
        // 提交订单请求
        this.createOrder = function (order, success_func, error_func) {
            var orderinfo = order_res.save({}, order, success_func, error_func);
            return orderinfo;
        };

        // 查询订单
        this.getOrderById = function (orderid) {
            var orderinfo = order_res.get({id: orderid});
            return orderinfo;
        };
        // 查询订单详情
        this.getOrderByOrderId = function (orderid,success,error) {
            var OrderByOrderId = $resource(mallorder+"orders/byorderid/:orderid");
            return OrderByOrderId.get({orderid:orderid},success,error);
        };
        // 查询某用户的所有详情
        this.selectOrder = function (page) {
            return order_res1.query({id: userid, num: page.num, n: page.n});
        };
        //根据订单完成状态和支付状态查询订单
        this.selectOrders = function (status) {
            return order_res1.query({
                buyid: userid,
                status: status.status,
                paystate: status.paystate,
                num: status.num,
                n: status.n
            });
        };
        //根据订单状态查询订单
        this.selectByOrderState = function (status) {
            return order_status.query({bybuyid: userid, status: status.status, num: status.num, n: status.n});
        };
        //根据支付状态查询订单
        this.selectByPayState = function (paystate) {
            return order_paystatus.query({
                bybuyid: userid,
                paystate: paystate.paystate,
                num: paystate.num,
                n: paystate.n
            });
        };

        // 获取用户订单列表
        this.getUserOrderList = function (userid) {
            return order_users.query({id: userid});
        };

        //更新订单数据
        this.updateorder = function (order, success_func, error_func) {
            console.log(order);
            return update.save({orderid: order.orderid}, order, success_func, error_func);
        };

        //删除已完成订单

        this.deleteOrder = function (orderid, success_func, error_func) {
            return update.remove({orderid: orderid}, success_func, error_func);
        };
        //缓存订单信息
        this.cacheOrder = function (order) {
            $storage.order = order;
            return $storage.order;
        };

        this.getCacheOreder = function () {
            return $storage.order;
        };
        this.delcache = function () {
            $storage.order = '';
        };

        //缓存订单信息
        this.cacheOrderid = function (orderid) {
            $storage.orderid = orderid;
            return $storage.orderid;
        };

        this.getCacheOrederid = function () {
            return $storage.orderid;
        };
        this.delcacheid = function () {
            $storage.orderid = '';
        };
        this.testOrder = function (orderid,success, error) {
            console.log(orderid);
            var resource = $resource(mallpay_url + "ordersdetail/:orderid");
            return resource.save({orderid: orderid}, success, error);
        }
    }])
;
